package github.kawaiior.yuanchuxiuzhen.common.tileentity;

import github.kawaiior.yuanchuxiuzhen.Yuanchuxiuzhen;
import github.kawaiior.yuanchuxiuzhen.common.container.GongFaCreateContainer;
import github.kawaiior.yuanchuxiuzhen.common.item.misc.ManaStoneItem;
import github.kawaiior.yuanchuxiuzhen.core.init.ItemInit;
import github.kawaiior.yuanchuxiuzhen.core.init.TileEntityInit;
import net.minecraft.block.BlockState;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.Inventory;
import net.minecraft.inventory.container.Container;
import net.minecraft.inventory.container.INamedContainerProvider;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.tileentity.ITickableTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityType;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TranslationTextComponent;

import javax.annotation.Nullable;

public class GongFaCreateTileEntity extends TileEntity implements ITickableTileEntity, INamedContainerProvider {

    private int tickTime = 0;
    private boolean isEmpty = true;
    public static final int LENGTH = 5;
    private Inventory manaInventory = new Inventory(1);
    private Inventory gongfaInventory = new Inventory(LENGTH);

    public GongFaCreateTileEntity() {
        this(TileEntityInit.GONG_FA_CREATE_TILE_ENTITY.get());
    }

    public GongFaCreateTileEntity(TileEntityType<?> tileEntityTypeIn) {
        super(tileEntityTypeIn);
    }

    @Override
    public ITextComponent getDisplayName() {
        return new TranslationTextComponent("container."+ Yuanchuxiuzhen.MODID+".example_gui_block");
    }

    @Nullable
    @Override
    public Container createMenu(int id, PlayerInventory playerInventory, PlayerEntity player) {
        return new GongFaCreateContainer(id, playerInventory, this);
    }

    @Override
    public void tick() {
        tickTime++;
        if (tickTime<20){
            return;
        }
        tickTime = 0;

        ItemStack itemStack = this.manaInventory.getStackInSlot(0);
        if (itemStack.isEmpty() || !(itemStack.getItem() instanceof ManaStoneItem) || itemStack.getCount() < 16){
            if (!isEmpty) {
                this.gongfaInventory.setInventorySlotContents(0, ItemStack.EMPTY);
                this.gongfaInventory.setInventorySlotContents(1, ItemStack.EMPTY);
                this.gongfaInventory.setInventorySlotContents(2, ItemStack.EMPTY);
                this.gongfaInventory.setInventorySlotContents(3, ItemStack.EMPTY);
                this.gongfaInventory.setInventorySlotContents(4, ItemStack.EMPTY);
                isEmpty = true;
            }
            return;
        }

        if (isEmpty){
            isEmpty = false;
        }

        int level = ((ManaStoneItem)itemStack.getItem()).getLevel();
        switch (level) {
            case 3:
                this.gongfaInventory.setInventorySlotContents(0, new ItemStack(ItemInit.JIN_YAO_JUE_ITEM.get()));
                this.gongfaInventory.setInventorySlotContents(1, new ItemStack(ItemInit.QING_MU_JUE_ITEM.get()));
                this.gongfaInventory.setInventorySlotContents(2, new ItemStack(ItemInit.XUAN_SHUI_JUE_ITEM.get()));
                this.gongfaInventory.setInventorySlotContents(3, new ItemStack(ItemInit.CHI_YAN_JUE_ITEM.get()));
                this.gongfaInventory.setInventorySlotContents(4, new ItemStack(ItemInit.YAN_TU_JUE_ITEM.get()));
                break;
            case 6:
                this.gongfaInventory.setInventorySlotContents(0, new ItemStack(ItemInit.JIN_GUANG_XUAN_GONG_ITEM.get()));
                this.gongfaInventory.setInventorySlotContents(1, new ItemStack(ItemInit.BAI_CAO_ZHEN_JING_ITEM.get()));
                this.gongfaInventory.setInventorySlotContents(2, new ItemStack(ItemInit.YOU_SHUI_ZHEN_JING_ITEM.get()));
                this.gongfaInventory.setInventorySlotContents(3, new ItemStack(ItemInit.FEN_JIN_CHI_HUO_JUE_ITEM.get()));
                this.gongfaInventory.setInventorySlotContents(4, new ItemStack(ItemInit.QING_SHAN_ZHEN_DI_GONG_ITEM.get()));
                break;
            case 9:
                // TODO: 更高级别的功法
            default:
                this.gongfaInventory.setInventorySlotContents(0, ItemStack.EMPTY);
                this.gongfaInventory.setInventorySlotContents(1, ItemStack.EMPTY);
                this.gongfaInventory.setInventorySlotContents(2, ItemStack.EMPTY);
                this.gongfaInventory.setInventorySlotContents(3, ItemStack.EMPTY);
                this.gongfaInventory.setInventorySlotContents(4, ItemStack.EMPTY);
                break;
        }
    }

    @Override
    public void read(BlockState state, CompoundNBT nbt) {
        for (int i = 0; i < LENGTH; i++) {
            this.gongfaInventory.setInventorySlotContents(i, ItemStack.read(nbt.getCompound("gongfa_"+i)));
        }
        this.manaInventory.setInventorySlotContents(0, ItemStack.read(nbt.getCompound("mana_item")));
        super.read(state, nbt);
    }

    @Override
    public CompoundNBT write(CompoundNBT compound) {
        for (int i = 0; i < LENGTH; i++) {
            ItemStack itemStack = this.gongfaInventory.getStackInSlot(i).copy();
            compound.put("gongfa_"+i, itemStack.serializeNBT());
        }

        compound.put("mana_item", this.manaInventory.getStackInSlot(0).copy().serializeNBT());
        return super.write(compound);
    }

    public Inventory getManaInventory() {
        return manaInventory;
    }

    public Inventory getGongfaInventory() {
        return gongfaInventory;
    }
}
